package com.lh0811.forge.pserv.uacs.server.adapter.controller;

import com.lh0811.forge.dependency.basic_web.constant.AuthConstant;
import com.lh0811.forge.dependency.basic_web.response.ServerResponse;
import com.lh0811.forge.dependency.security.client.component.SecurityUtil;
import com.lh0811.forge.dependency.security.client.component.annotation.TokenToAuthority;
import com.lh0811.forge.dependency.security.core.model.entity.SecurityAuthority;
import com.lh0811.forge.dependency.security.core.model.param.TokenParam;
import com.lh0811.forge.dependency.security.core.model.vo.AuthenticationVo;
import com.lh0811.forge.dependency.security.server.SecurityServer;
import com.lh0811.forge.pserv.uacs.server.config.auth.phone_pwd.PhonePasswordVerification;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author lh0811
 * @date 2021/5/23
 */
@Slf4j
@Tag(name = "权限中心-通用接口")
@RestController
@RequestMapping("/forge/plate/v1/uacs/security")
public class CommAuthorizeController {

    @Resource
    private SecurityServer securityServer;


    @PostMapping("/login/phone_password")
    @Operation(summary = "系统用户登录")
    public ServerResponse<AuthenticationVo> sysUserLoginByUsernamePassword(@Validated @RequestBody PhonePasswordVerification param) throws Exception {
        AuthenticationVo authorize = securityServer.authorize(param);
        return ServerResponse.createBySuccess("登录成功", authorize);
    }

    @TokenToAuthority
    @GetMapping("/logout")
    @Operation(summary = "用户退出", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse logout() throws Exception {
        SecurityAuthority securityAuthority = SecurityUtil.getSecurityAuthority();
        securityServer.tokenDestroy(new TokenParam(securityAuthority.getToken()));
        return ServerResponse.createBySuccess("退出成功");
    }


}
